<template>
  <div>
    <cardTool>
      <template #headerDiy>
        <el-row style="width:100%" type="flex" justify="space-between">
          <el-button size="small" type="primary" @click="addFn">添加页面</el-button>
          <el-row type="flex">
            <el-input v-model="title_contains" style="width:700px" placeholder="请输入内容" />
            <el-button size="small" @click="find">筛选</el-button>
          </el-row>
        </el-row>
      </template>
      <template #body>
        <el-table
          border
          :header-cell-style="headerCellStyle"
          :cell-style="cellStyle"
          :data="tableData"
          stripe
          style="width: 100%"
        >
          <el-table-column
            prop="title"
            label="标题"
          />
          <el-table-column
            prop="content"
            label="内容"
          >
            <template #default="{row}">
              <span v-html="row.content" />
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button type="primary" size="small" @click="update(row.id)">编辑</el-button>
              <el-button type="warning" size="small" @click="delFn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="center">
          <el-pagination
            style="margin-top:10px"
            :page-size="limit"
            layout="prev, pager, next, jumper"
            :total="total"
            background
            @current-change="handleCurrentChange"
          />
        </el-row>
      </template>
    </cardTool>
    <Addpage ref="addPage" :show-dialog.sync="visibleDialog" @addPages="addPage" @updatePages="updatePage" />
  </div>
</template>

<script>
import { getPageList, addPage, deletePage, getPageById, updatePage, findPage, getPageNumber } from '@/api/page'
import Addpage from './common/Add-page.vue'
export default {
  components: {
    Addpage
  },
  data() {
    return {
      isShow: true,
      tableData: [],
      limit: 5,
      start: 0,
      visibleDialog: false,
      title_contains: '',
      total: 100

    }
  },

  created() {
    this.getPageList()
    this.getNumber()
  },
  methods: {
    headerCellStyle() {
      return 'text-align:center'
    },
    cellStyle() {
      return 'text-align:center'
    },
    // 分页功能
    handleCurrentChange(val) {
      this.start = val * this.limit - this.limit

      this.getPageList()
    },

    // 获取页面列表 封装函数
    async getPageList() {
      const res = await getPageList({
        _limit: this.limit,
        _start: this.start
      })
      // console.log(83, res)
      this.tableData = res.data
    },
    // 打开弹窗
    addFn() {
      this.visibleDialog = true
    },
    // 子组件传递过来的增加页面的提交事件
    async addPage(formData) {
      await addPage(formData)
      this.getNumber()
      this.getPageList()
      this.$refs.addPage.closeFn()
    },
    // 删除页面
    async delFn(id) {
      this.$confirm('确定删除吗？', '提示', {
        type: 'warning'
      }).then(async() => {
        if (this.tableData.length === 1) {
          this.start = this.start - this.limit
        }
        await deletePage(id)
        this.getNumber()
        this.getPageList()
      }).catch(err => {
        console.log(err)
      })
    },
    // 根据id获取单个页面详情 实现数据回显
    async update(id) {
      this.visibleDialog = true
      const res = await getPageById(id)
      // console.log(104, res)
      this.$refs.addPage.formData = res.data
    },
    // 提交更新后的数据
    async updatePage(formData) {
      // console.log(formData)
      await updatePage(formData)
      // console.log(112, res)
      this.getPageList()
      this.$refs.addPage.closeFn()
    },
    // 模糊查找
    async find() {
      const res = await findPage({
        title_contains: this.title_contains
      })
      // console.log(112, res)
      this.tableData = res.data
    },
    // 获取总数
    async getNumber() {
      const res = await getPageNumber()
      // console.log(69, res)
      this.total = res.data.length
    }
  }
}
</script>

<style>

</style>
